//
//  VisibleView.swift
//  WEIBO_SWIFT
//
//  Created by 岳挺 on 15/6/24.
//  Copyright © 2015年 yueting. All rights reserved.
//

import UIKit

protocol VisibleViewDelegate : NSObjectProtocol{
    func visibleViewRegisterBtnDidClick()
    func visibleViewLoginBtnDidClick()
}

class VisibleView: UIView {
    
    weak var delegate :VisibleViewDelegate?
    
    
    override init(frame: CGRect){
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 0.93, alpha: 1.0)
        
        
        setSubviews()
    }
    
    required init(coder aDecoder: NSCoder) {
        print(__FUNCTION__)
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setUpSubviewInfo (imageName: String, message: String, isHome:Bool) {
        introLabel.text = message
        if isHome {
            iconImage.hidden = false
            imageView.image = UIImage(named: "visitordiscover_feed_image_house")
        }else{
            iconImage.hidden = true
            imageView.image = UIImage(named: imageName)
        }
    }
    
    //iconimage旋转方法
    func iconImageRotate (){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20.0
        animation.repeatCount = MAXFLOAT
        iconImage.layer.addAnimation(animation, forKey: nil)
    }

    func setSubviews (){
        addSubview(iconImage)
        addSubview(coverView)
        addSubview(imageView)
        addSubview(introLabel)
        addSubview(register)
        addSubview(login)
        
        iconImage.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -80))
        
        coverView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|[subview]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview":coverView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[subview]-240-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview":coverView]))
        
        imageView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        introLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: introLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: introLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subview(240)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views:["subview":introLabel]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(80)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview":introLabel]))
        
        register.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: register, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: introLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: register, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: introLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subview(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview":register]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(50)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview":register]))
        
        login.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: login, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: introLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: login, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: introLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[subview(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview":login]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[subview(50)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["subview":login]))
    }
    
    //动画图片的懒加载
    lazy var iconImage : UIImageView = {
        let icon = UIImageView()
        icon.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        icon.frame = CGRectMake(0, 0, 90, 90)
        return icon
        }()
    
    //遮罩懒加载
    lazy var coverView : UIImageView = {
        let cover = UIImageView()
        cover.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return cover
        }()
    
    lazy var imageView : UIImageView = {
        let icon = UIImageView()
        icon.image = UIImage(named: "visitordiscover_feed_image_house")
        icon.frame = CGRectMake(0, 0, 94, 90)
        return icon
        }()
    
    //文字信息懒加载
    lazy var introLabel : UILabel = {
        let label = UILabel()
        label.text = "come on babycome on babycome on babycome on babycome on babycome on babycome on baby"
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(13.0)
        label.numberOfLines = 0;
        label.preferredMaxLayoutWidth = 320.0
        return label
        }()
    
    //注册按钮
    lazy var register : UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "registerAccoutn", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    
    //登陆按钮
    lazy var login : UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginAccount", forControlEvents: UIControlEvents.TouchUpInside)

        return btn
        }()
    
    //注册按钮监听方法
    func registerAccoutn(){
        delegate?.visibleViewRegisterBtnDidClick()
    }
    
    //登陆按钮监听
    func loginAccount (){        
        delegate?.visibleViewLoginBtnDidClick()
    }
    
    //遮罩
}
